// pages/searchCase/searchCase.js
import CaseStudyApi from '../../api/caseStudy.js'
import SpecialApi from "../../api/special";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    viewHeight:0,
    value:'',
    caseList:[],
    allCaseList:[],
    flag:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 页面首次渲染完毕时执行
    let height = wx.getSystemInfoSync().windowHeight
    let $navBar = this.selectComponent("#navBar")
    let navBarHeight = $navBar.data.navBarExtendHeight + $navBar.data.navBarHeight
    let viewHeight = height - navBarHeight
    this.setData({
      viewHeight:viewHeight
    })
    this.getCaseList()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  handlerGobackClick(){
    // wx.redirectTo({
    //   url: '/pages/caseStudy/caseStudy'
    // })
    wx.navigateBack()
  },
  onCancel(){
    wx.navigateBack()
  },
  /**
   * 查询病例列表
   */
  async getCaseList() {
    const param = {
      "gender": [],
      "hard": [],
      "likeKeyWord": '',
      "age":[],
      "spDepartmentId": []
    }
    const res = await CaseStudyApi.getCaseList(param)
    this.setData({
      allCaseList: res.data
    })
  },
  /**
   * 搜索病例
   */
  searchCase(e){
    let val = e.detail;
    this.setData({value: val})
    if (val.length > 0) {
      this.setData({caseList: []})
      let arr = [];
      for (let i = 0; i < this.data.allCaseList.length; i++) {
          if (this.data.allCaseList[i].name.indexOf(val) > -1) {
              arr.push(this.data.allCaseList[i])
          }
      }
      this.setData({caseList: arr,flag:true})
    } else {
      this.setData({caseList: [],flag:true})
    }
  },
  /**
   * 开始学习
   */
  startStudy(e){
    const id = e.currentTarget.dataset.id
    wx.redirectTo({
      url: `/pages/caseStudyTest/caseStudyTest?id=${id}`
    })
  }
})
